package com.site.service.impl.dining;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.site.dao.dining.RestaurantMenuDao;
import com.site.dto.dining.RestaurantMenuBizBean;
import com.site.model.dining.RestaurantMenuInfo;
import com.site.service.dining.RestaurantMenuService;
import com.site.util.PropertyUtils;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 餐厅菜单
 * @author jack
 *
 */
@Service
public class RestaurantMenuServiceImpl implements RestaurantMenuService{

	private RestaurantMenuDao restaurantMenuDao;
	
	@Resource
	public void setRestaurantMenuDao(RestaurantMenuDao restaurantMenuDao) {
		this.restaurantMenuDao = restaurantMenuDao;
	}

	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 增加
		if(COMMAND_ADD.equals(command)){
			add((RestaurantMenuBizBean)obj);
		// 修改
		} else if(COMMAND_UPDATE.equals(command)){
			eidt((RestaurantMenuBizBean)obj);
		// 删除
		} else if(COMMAND_DELETE.equals(command)){
			delete((List<RestaurantMenuBizBean>)obj);
		}
		return null;
	}

	/**
	 * 删除
	 * @param obj
	 */
	private void delete(List<RestaurantMenuBizBean> obj) {
		if(null != obj && !obj.isEmpty()){
			for (RestaurantMenuBizBean restaurantMenuBizBean : obj) {
				if(null != restaurantMenuBizBean && restaurantMenuBizBean.getId() != null){
					restaurantMenuDao.delete(RestaurantMenuInfo.class, restaurantMenuBizBean.getId());
				}
			}
		}
	}

	/**
	 * 修改
	 * @param obj
	 */
	private void eidt(RestaurantMenuBizBean obj) {
		if(null != obj && obj.getId() != null){
			RestaurantMenuInfo restaurantMenuInfo = restaurantMenuDao.getEntityById(RestaurantMenuInfo.class, obj.getId());
			PropertyUtils.copyNotNullProperties(restaurantMenuInfo, obj);
			restaurantMenuDao.update(restaurantMenuInfo);
		}
	}

	/**
	 * 增加
	 * @param obj
	 */
	private void add(RestaurantMenuBizBean obj) {
		if(null != obj){
			RestaurantMenuInfo restaurantMenuInfo = new RestaurantMenuInfo();
			PropertyUtils.copyProperties(restaurantMenuInfo, obj);
			restaurantMenuInfo.setRmno(restaurantMenuDao.getMaxId("CD", 4, RestaurantMenuInfo.class));
			restaurantMenuDao.save(restaurantMenuInfo);
		}
	}

	@Override
	public List<RestaurantMenuBizBean> getRestaurantMenus(
			QueryPage queryPage, RestaurantMenuBizBean restaurantMenuBizBean) {
		List<RestaurantMenuInfo> restaurantMenuInfos =  restaurantMenuDao.getRestaurantMenus(queryPage, restaurantMenuBizBean);
		List<RestaurantMenuBizBean> restaurantMenuBizBeans = new ArrayList<RestaurantMenuBizBean>();
		if(null !=restaurantMenuInfos && !restaurantMenuInfos.isEmpty()){
			for (RestaurantMenuInfo restaurantMenuInfo : restaurantMenuInfos) {
				if(null != restaurantMenuInfo){
					RestaurantMenuBizBean menuBizBean = new RestaurantMenuBizBean();
					PropertyUtils.copyProperties(menuBizBean, restaurantMenuInfo);
					restaurantMenuBizBeans.add(menuBizBean);
				}
			}
		}
		if(null != restaurantMenuBizBeans && !restaurantMenuBizBeans.isEmpty()){
			return restaurantMenuBizBeans;
		}
		return null;
	}

	@Override
	public RestaurantMenuBizBean getEntityById(Long id) {
		if(null != id){
			RestaurantMenuInfo restaurantMenuInfo = restaurantMenuDao.getEntityById(RestaurantMenuInfo.class, id);
			if(restaurantMenuInfo != null){
				RestaurantMenuBizBean restaurantMenuBizBean = new RestaurantMenuBizBean();
				PropertyUtils.copyProperties(restaurantMenuBizBean, restaurantMenuInfo);
				return restaurantMenuBizBean;
			}
		}
		return null;
	}
}
